using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia;
using System.Threading.Tasks;
using Avalonia.Controls.Primitives;
using Avalonia.Layout;
using System;
using System.Windows.Input;

namespace SerialTool.Services;

public class DialogService
{
    private static DialogService? _instance;
    private DialogService() { }

    public static DialogService Instance => _instance ??= new DialogService();

    private Window? GetMainWindow()
    {
        return (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
    }

    /// <summary>
    /// 显示消息对话框
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="message">消息内容</param>
    /// <returns>异步任务</returns>
    public async Task ShowMessageAsync(string title, string message)
    {
        var mainWindow = GetMainWindow();
        if (mainWindow == null) return;
        
        var messageBox = new Window
        {
            Title = title,
            Width = 400,
            Height = 200,
            WindowStartupLocation = WindowStartupLocation.CenterOwner,
            Content = new StackPanel
            {
                Margin = new Thickness(20),
                Spacing = 20,
                Children = 
                {
                    new TextBlock 
                    { 
                        Text = message, 
                        TextWrapping = Avalonia.Media.TextWrapping.Wrap 
                    },
                    new Button
                    {
                        Content = "确定",
                        HorizontalAlignment = HorizontalAlignment.Right,
                        Command = new RelayCommand(() => {})
                    }
                }
            }
        };
        
        if (messageBox.Content is StackPanel panel && 
            panel.Children.Count > 1 && 
            panel.Children[1] is Button okButton)
        {
            okButton.Click += (sender, e) => messageBox.Close();
        }
        
        await messageBox.ShowDialog(mainWindow);
    }

    /// <summary>
    /// 显示确认对话框
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="message">消息内容</param>
    /// <param name="okText">确认按钮文本</param>
    /// <param name="cancelText">取消按钮文本</param>
    /// <returns>用户是否确认</returns>
    public async Task<bool> ShowConfirmationAsync(string title, string message, string okText = "确定", string cancelText = "取消")
    {
        var mainWindow = GetMainWindow();
        if (mainWindow == null) return false;
        
        var result = false;
        var confirmationDialog = new Window
        {
            Title = title,
            Width = 400,
            Height = 200,
            WindowStartupLocation = WindowStartupLocation.CenterOwner,
            Content = new StackPanel
            {
                Margin = new Thickness(20),
                Spacing = 20,
                Children = 
                {
                    new TextBlock 
                    { 
                        Text = message, 
                        TextWrapping = Avalonia.Media.TextWrapping.Wrap 
                    },
                    new StackPanel
                    {
                        Orientation = Orientation.Horizontal,
                        HorizontalAlignment = HorizontalAlignment.Right,
                        Spacing = 10,
                        Children =
                        {
                            new Button
                            {
                                Content = cancelText
                            },
                            new Button
                            {
                                Content = okText
                            }
                        }
                    }
                }
            }
        };
        
        if (confirmationDialog.Content is StackPanel panel && 
            panel.Children.Count > 1 && 
            panel.Children[1] is StackPanel buttonPanel)
        {
            if (buttonPanel.Children.Count > 0 && buttonPanel.Children[0] is Button cancelButton)
            {
                cancelButton.Click += (sender, e) => 
                {
                    result = false;
                    confirmationDialog.Close();
                };
            }
            
            if (buttonPanel.Children.Count > 1 && buttonPanel.Children[1] is Button okButton)
            {
                okButton.Click += (sender, e) => 
                {
                    result = true;
                    confirmationDialog.Close();
                };
            }
        }
        
        await confirmationDialog.ShowDialog(mainWindow);
        return result;
    }
    
    private class RelayCommand : ICommand
    {
        private readonly Action _execute;
        
        public RelayCommand(Action execute)
        {
            _execute = execute;
        }
        
#pragma warning disable CS0067 // Event is never used
        public event EventHandler? CanExecuteChanged;
#pragma warning restore CS0067
        
        public bool CanExecute(object? parameter) => true;
        
        public void Execute(object? parameter) => _execute();
    }
} 